package com.bridgeintelligent.tag.scene.mapper;

import com.bridgeintelligent.tag.scene.api.entity.Scene;
import com.bridgeintelligent.tag.scene.api.entity.SceneCatalog;
import com.bridgeintelligent.tag.scene.api.entity.SceneStep;
import com.bridgeintelligent.tag.scene.api.vo.*;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @description
 * @author: grp 2021-01-10 22:29
 **/
@Mapper
public interface SceneMapper {
    @Update("update tag_scene set catalog_id = #{catalogId},update_time=#{updateTime,jdbcType=TIMESTAMP} where id = #{id}")
    int updateCatalogIdById(@Param("id") String id, @Param("catalogId")String catalogId,
                            @Param("updateTime") Date updateTime);

    List<Scene> findByCatalogId(String id);

    int add(Scene base);

    int addSteps(List<SceneStep> steps);

    int addSingleStep(SceneStep steps);

    @Select("select customers_id id, customers_name name from tag_customers where D_STATUS = '01' and APPROVAL_STATUS = '4'")
    List<CustomersVO> findCustomers();

    @Select("select dim_id id, dim_name name from dim_data a where DIM_LARGE_TYPE = '04' and state = '01' " +
            " and (CREATER_ID = #{userId} or SHARED_STATE = '01' or (SHARED_STATE = '02' and (" +
            " (CREATER_ID <> #{userId} and exists(select 1 from TAG_SHARE_RELATION r where" +
            " r.dim_id = a.dim_id and r.user_id = #{userId})))))")
    List<CombinationTagVO> findCombinations(String userId);

    @Delete("delete from tag_scene_step where scene_id = #{id}")
    int deleteStepsBySceneId(@Param("id") String id);

    int updateById(Scene base);

    List<SceneVO> findList(@Param("catalogList") List<SceneCatalog> catalogList, @Param("name") String name,
                           @Param("start") String start, @Param("end") String end,
                           @Param("type") String type, @Param("userId")String userId);

    @Delete("delete from tag_scene where id = #{id}")
    int deleteById(String id);

    @Select("select count(1) from tag_scene where name = #{name}")
    int countByName(String name);

    @Select("select count(1) from tag_scene where name = #{name} and id <> #{id}")
    int countByNameAndNotId(String name, String id);

    SceneVO findById(String id);

    Scene findOneScene(String id);

    List<SceneStepVO> findStepsBySceneId(String id);

    int countBySort(Integer sort);

    List<Scene> findBySort(Integer sort);

    int unTop(String id);

    int top(String id, Integer top);

    Scene findUpMate(Integer sort);

    Scene findDownMate(Integer sort);

    int updateSort(String id);

    List<SceneVO> findByParentId(String id);

    @Select("select dim_id id, dim_name name from dim_data a where DIM_LARGE_TYPE = '02' and approve_state = '4' " +
            "and state = '01' and (CREATER_ID = #{userId} or SHARED_STATE = '01' or (SHARED_STATE = '02' and (" +
            " (CREATER_ID <> #{userId} and exists(select 1 from TAG_SHARE_RELATION r where" +
            " r.dim_id = a.dim_id and r.user_id = #{userId})))))")
    List<FreeTagVO> findFrees(String userId);

    Scene findUpMate2(Integer sort, List<SceneCatalog> catalogList, String name, String start, String end);

    @Update("update tag_scene set sort=sort+#{offset} where sort> #{sort1} and sort <#{sort2}")
    int move(int offset, Integer sort1, Integer sort2);

    Scene findDownMate2(Integer sort, List<SceneCatalog> catalogList, String name, String start, String end);
}
